<template>
  <el-dialog
    :title="`学员【${studentName}】备注`"
    :visible.sync="visible"
    width="60%"
    size="mini"
    @close="handleClose"
  >
    <el-form ref="remarkForm" :model="remarkForm" label-width="80px" :rules="remarkRules" class="remark-form">
      <el-form-item label="备注内容" prop="remarks">
        <el-input
          v-model="remarkForm.remarks"
          type="textarea"
          :rows="4"
          placeholder="请输入备注内容"
          style="width: 100%"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item label="备注历史" label-width="80px">
        <el-table
          :data="remarkHistory"
          border
          stripe
          size="mini"
          :cell-style="{ 'vertical-align': 'middle' }"
          style="width: 100%;"
          :loading="remarkLoading"
          empty-text="暂无备注记录"
        >
          <el-table-column prop="time" label="时间" align="center" width="180" />
          <el-table-column prop="operator" label="备注人" align="center" width="120" />
          <el-table-column prop="content" label="备注" align="left" />
        </el-table>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取消</el-button>
        <el-button size="mini" type="primary" @click="submitRemark" :loading="remarkSubmitting">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { listRemarksByParams, addRemarks } from "@/api/training/remarks";

export default {
  name: "StudentRemarkDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    studentId: {
      type: [String, Number],
      required: true
    },
    studentName: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      remarkForm: { remarks: "", studentId: this.studentId },
      remarkHistory: [],
      remarkLoading: false,
      remarkSubmitting: false,
      remarkRules: {
        remarks: [{ required: true, message: "请输入备注内容", trigger: "blur" }]
      }
    };
  },
  watch: {
    studentId(newVal) {
      this.remarkForm.studentId = newVal;
      this.loadRemarkHistory();
    },
    visible(newVal) {
      if (newVal) {
        this.loadRemarkHistory();
      }
    }
  },
  methods: {
    async loadRemarkHistory() {
      if (!this.studentId) return;

      this.remarkLoading = true;
      try {
        const res = await listRemarksByParams({
          foreignId: this.studentId,
          remarkType: "4" // 学员备注类型
        });
        this.remarkHistory = res.rows.map(item => ({
          time: item.createTime,
          operator: item.createBy,
          content: item.remarks
        }));
      } catch (err) {
        this.$message.error('加载备注历史失败：' + (err.msg || err.message));
        this.remarkHistory = [];
      } finally {
        this.remarkLoading = false;
      }
    },
    async submitRemark() {
      this.$refs.remarkForm.validate(async valid => {
        if (valid) {
          this.remarkSubmitting = true;
          try {
            const res = await addRemarks({
              foreignId: this.studentId,
              remarkType: "4",
              remarks: this.remarkForm.remarks
            });
            if (res.code === 200) {
              this.$message.success('备注提交成功');
              this.$emit('remark-updated');
              this.visible = false;
            } else {
              this.$message.error(res.msg || '备注提交失败');
            }
          } catch (err) {
            this.$message.error('备注提交失败：' + (err.msg || err.message));
          } finally {
            this.remarkSubmitting = false;
          }
        }
      });
    },
    handleClose() {
      this.$emit('remark-updated');
      this.$nextTick(() => {
        if (this.$refs.remarkForm) this.$refs.remarkForm.resetFields();
        this.remarkForm.remarks = "";
      });
    }
  }
};
</script>

<style scoped>
::v-deep .dialog-remark .el-textarea {
  width: 100%;
}
::v-deep .dialog-remark .el-table {
  margin-top: 10px;
}
</style>
